package com.zhang.springbootdemo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhang.springbootdemo.pojo.Employee;
import com.zhang.springbootdemo.service.EmployeeService;
import com.zhang.springbootdemo.vo.DepEmp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.events.Event;

import java.util.List;

@Controller
@RequestMapping("/oper")
public class OperaEmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 增加员工时调用查询并返回到增加页面
     * @return
     */
    @GetMapping("/addEmp")
    public String add(Model model){
        List<DepEmp> employees = employeeService.selAll();
        model.addAttribute("employees",employees);
        return "add";
    }

    /**
     * 保存员工数据
     * @param employee
     * @return
     */
    @PostMapping("/addEmp")
    public String save(Employee employee){
        employeeService.addEmp(employee);

        return "redirect:/sel";
    }

    /**
     * 删除员工
     * @param id
     * @return
     */

    @RequestMapping("/del/{id}")
    public String del(@PathVariable("id") Integer id){
        employeeService.delEmpByid(id);
        return "redirect:/sel";
    }

    /**
     * 修改员工
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id,Model model){
        DepEmp depEmp = employeeService.selAllById(id);
        System.out.println(depEmp);
        model.addAttribute("depEmp",depEmp);
        List<DepEmp> depEmps = employeeService.selAll();
        model.addAttribute("depEmps",depEmps);
        return "edit";
    }

    /**
     * 修改保存
     * @param
     * @return
     */
    @RequestMapping("/edit")
    public String editSave(DepEmp depEmp){
        System.out.println(depEmp.toString());
        employeeService.editEmps(depEmp);
        return "redirect:/sel";
    }


    @GetMapping("/pagehelper")
    @ResponseBody
    public PageInfo<Employee> list(int pageNum, int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Employee> lists = employeeService.selEmp();
        PageInfo<Employee> pageInfo =new PageInfo<>(lists);
        return pageInfo;

    }
}
